/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

'Gio.Upload.Uploader'.namespace();

/**
 * This uploader shows the progress bar at the right-top corner
 * It requires:
 * - jquery.uploadify.js
 * - Gio.Core.Message
 * 
 * @param string inputId Id of file element
 */
Gio.Upload.Uploader = function(inputId) {
	this._inputId     = inputId;
	this._buttonLabel = 'Upload';
	return this;
};

/**
 * Formats file size
 * 
 * @param int size
 * @return array with two keys:
 * - suffix: KB or MB
 * - size: The file size converted in the suffix
 */
Gio.Upload.Uploader.formatSize = function(size) {
	size       = Math.round(size / 1024 * 100) * .01;
	var suffix = 'KB';
	if (size > 1000) {
		size   = Math.round(size * .001 * 100) * .01;
		suffix = 'MB';
	}
	var sizeParts = size.toString().split('.');
	if (sizeParts.length > 1) {
		size = sizeParts[0] + '.' + sizeParts[1].substr(0, 2);
	} else {
		size = sizeParts[0];
	}
	return { size: size, suffix: suffix };
};

Gio.Upload.Uploader.prototype = {
	/**
	 * Setters
	 */
	
	/**
	 * @param array uploadSettings Uploadify settings
	 * @return Gio.Upload.Uploader
	 */
	setUploadSettings: function(uploadSettings) {
		this._uploadSettings = uploadSettings;
		return this;
	},

	/**
	 * @param array messageSettings Settings for showing the message
	 * @return Gio.Upload.Uploader
	 */
	setMessageSettings: function(messageSettings) {
		this._messageSettings = messageSettings;
		return this;
	},
	
	/**
	 * Sets label for upload button
	 *  
	 * @param string label
	 * @return Gio.Upload.Uploader
	 */
	setButtonLabel: function(label) {
		this._buttonLabel = label;
		return this;
	},
	
	/**
	 * Render the upload section
	 * 
	 * @return void
	 */
	show: function() {
		var self            = this;
		var options 		= this._uploadSettings;
		var messageSettings = this._messageSettings;
		var inputId			= this._inputId;
		
		options.onSelect = function(event, ID, fileObj) {
			/** Format file name */
			var name = fileObj.name;
			if (name.length > 20) {
				name = name.substr(0, 20) + '...';
			}
			
			/** Format file size */
			var fileSize = Gio.Upload.Uploader.formatSize(fileObj.size);

			/** Title of message box */
			var title = [];
			title.push('<div class="g_a_left">', name, '</div>',
						'<div class="g_a_right">', 
							'<a href="javascript: void(0);" onclick="javascript: ' + $('#' + ID).uploadifyCancel(ID) + ';">',
								'<img src="', options.cancelImg, '" />', 
							'</a>',
						'</div>');

			/** Content of message box */
			var text = [];
			text.push('<div class="uploadifyProgress" style="clear: both">', 
		       				'<div id="', inputId, ID, 'ProgressBar" class="uploadifyProgressBar"></div>',
		       			'</div>',
		       			'<div id="', inputId, ID, 'Percentage">', fileSize.size, fileSize.suffix, '</div>');

			messageSettings.title = title.join('');
			messageSettings.text  = text.join('');
			
			/** Show the message box */
			//Gio.Core.Message.show(messageSettings, inputId + '_' + ID);
			Gio.Message.notice(messageSettings.text);
			
			return false;
		};
		
		options.onProgress = function(event, ID, fileObj, data) {
			$('#' + inputId + ID + 'ProgressBar').css('width', data.percentage + '%');
			
			var displayData = $('#' + inputId + ID + 'Percentage').html();
			if (settings.toDisplay == 'percentage') {
				displayData = ' - ' + data.percentage + '%';
			}
			if (settings.toDisplay == 'speed') {
				displayData = ' - ' + data.speed + 'KB/s';
			}
			$('#' + inputId + ID + 'Percentage').html(displayData);
		};
		
		var completeHandler = options.onComplete;
		if (completeHandler != null) {
			options.onComplete = function(event, ID, fileObj, response, data) {
				completeHandler(event, ID, fileObj, response, data);
				
				/**
				 * Remove the message box when uploading complete
				 */
			};
		}
		
		/**
		 * Styling uploadify button
		 * http://stackoverflow.com/questions/2483662/uploadify-button-style-with-css
		 */
		options.buttonImg = ' ';
		options.wmode	  = 'transparent';
		
		$('#' + inputId).wrap('<div class="g_a_upload_uploader" />')
						.wrap('<div class="g_a_upload_uploader_object_wrapper" />');
		
		var wrapper = $('#' + inputId).parents('.g_a_upload_uploader');
		$('<button/>').attr('title', $('#' + inputId).attr('title'))
					  .html('<span>' + this._buttonLabel + '</span>')
					  .prependTo($(wrapper));
		
		$('#' + inputId).uploadify(options);
		
		var width  = $(wrapper).find('button').outerWidth();
		var height = $(wrapper).find('button').outerHeight();
		$(wrapper).find('object').attr('width', width).attr('height', height);
		$(wrapper).css('width', width + 'px').css('height', height + 'px');
	}
};
